<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	    		xmlns:ui="http://java.sun.com/jsf/facelets"
	  			xmlns:h="http://java.sun.com/jsf/html"
	  			xmlns:f="http://java.sun.com/jsf/core"                
                xmlns:cs="http://myfaces.apress.com/custom"
				template="/WEB-INF/layouts/standard.xhtml"
				xmlns:a4j="http://richfaces.org/a4j"
				xmlns:rich="http://richfaces.org/rich"
				xmlns:c="http://java.sun.com/jstl/core"
				xmlns:bf="http://www.bssmanager.com/jsf">

	
	<ui:define name="headIncludes">
		<script>
			function checkOutConfirmation(pn) {
				return showConfirmation("Are you sure "+ pn +" ? ");
			}				
		</script>		
	</ui:define>
	
	<ui:define name="formheader">
		<h2>Attendent</h2>
	</ui:define>

	<ui:define name="content">
						
			<h:messages errorClass="errors" style="color:#ff1111" />
						
			<h:form id="attendentform" prependId="false">
			
					
			<h:panelGrid columns="1" width="100%">
			
			<rich:panel style="border:0">
				<f:facet name="header">
					<h:outputText value="Select Patient"/>
				</f:facet>
				<bf:admissionSelector controller="#{currentAttendentController}" selectMethod="selectAdmission" 
								reRender="selectedPatient1,selectedPatient2,selectedPatient3"/>			
			</rich:panel>
			
			<rich:panel id="selectedPatient1" style="display:#{currentAttendentController.admission.id le 0 ? 'none':'block' };border:0">
				<f:facet name="header">
					<h:outputText value="Patient Details"/>
				</f:facet>
				<h:panelGroup  style="display:#{currentAttendentController.admission.id le 0 ? 'none':'block' }">
					<h:outputText
						value="#{currentAttendentController.admission.dateOfExit == null ? '': 'Patient has Checked Out !'}"
						style="font-weight:bold; color:red" />
					<h:panelGrid columns="2">
						<h:outputLabel value="Name: " />
						<h:outputText value="#{currentAttendentController.admission.patient.name}" />
						<h:outputLabel value="Checked In: " />
						<h:outputText value="#{currentAttendentController.admission.dateOfAdmission}">
							<f:convertDateTime pattern="dd MMM yyyy HH:MM" />
						</h:outputText>							
					</h:panelGrid>				
				</h:panelGroup>				
			</rich:panel>									
										
			<rich:panel id="selectedPatient2" style="display:#{currentAttendentController.admission.id le 0 ? 'none':'block' };border:0">
				<f:facet name="header">
					<h:outputText value="Add Attendent"/>
				</f:facet>																
				<h:panelGroup>		
					<bf:attendent model="#{currentAttendentController.model}"
								addMethod="add"
								removeMethod="exitAttendent"
								selectMethod="selectAttendent"
								existing="attendents"
								reRender="selectedPatient3,selectedPatient2,attendentErrorsWindow"
								data="#{currentAttendentController.data}"			
								controller="#{currentAttendentController}"
								onAddComplete="if(#{currentAttendentController.error})Richfaces.showModalPanel('attendentErrorsWindow');"
								requiredEditing="true"
								askConfirmation="true"/>
				</h:panelGroup>
			</rich:panel>								
								
				<rich:panel id="selectedPatient3" style="display:#{currentAttendentController.admission.id le 0 ? 'none':'block' };border:0">
					<f:facet name="header">
						<h:outputText value="All Attendents"/>
					</f:facet>
					
					<rich:dataTable id="allAttendents" rowKeyVar="rowkey" rows="10"
									value="#{currentAttendentController.allAttendents}" var="aBean" align="center">										
						<rich:column>
							<f:facet name="header">S/N</f:facet>
							<h:outputText value="#{rowkey + 1} "/>
						</rich:column>	
						<rich:column>
							<f:facet name="header">Check in</f:facet>
							<a4j:commandButton value="+" action="#{currentAttendentController.checkin}" 
													reRender="selectedPatient2"
													disabled="#{currentAttendentController.editing}"
													title="#{currentAttendentController.editing ? 'Edit in progress...':'Click to checkin'}"
													onclick="if(!showConfirmation('Are you sure to checkin #{aBean.name} ?'))return false;showLoading();"
													style="width:15px;height:15px;#{currentAttendentController.editing ? 'cursor: not-allowed':''}"
													oncomplete="hideLoading();if(#{currentAttendentController.error})alert('#{currentAttendentController.errors[0]}');"/>
						</rich:column>										
						<rich:column>
							<f:facet name="header">Name</f:facet>
							<h:outputText value="#{aBean.name} "/>
						</rich:column>
						<rich:column>
							<f:facet name="header">Relationship</f:facet>
							<h:outputText value="#{aBean.relationship}" converter="#{relationshipConverter}"/>
						</rich:column>																	
						<rich:column>
							<f:facet name="header">Sex</f:facet>
							<h:outputText value="#{aBean.sex == 'M' ? 'Male' : 'Female'} "/>
						</rich:column>
						<rich:column>
							<f:facet name="header">Date of Birth</f:facet>
							<h:outputText value="#{aBean.dateOfBirth}">
								<f:convertDateTime pattern="dd MMM yyyy" timeZone="#{preferences.timeZone.ID}"/>
							</h:outputText>
						</rich:column>								
						<rich:column>
							<f:facet name="header">Phone</f:facet>
							<h:outputText value="#{aBean.phone} "/>
						</rich:column>																		
						<f:facet name="footer">
							<h:outputText rendered="#{empty currentAttendentController.allAttendents}" value="No Attendents Added..." />
							<rich:datascroller rendered="#{not empty currentAttendentController.allAttendents}"/>
						</f:facet>		
					</rich:dataTable>																
				</rich:panel>								
			</h:panelGrid>
								
			</h:form>
			
		
		
		<rich:modalPanel id="attendentErrorsWindow" minWidth="300" zindex="2000" resizeable="false" autosized="true" >
			<f:facet name="header">
				<h:outputText value="Attendant Error"/>
			</f:facet>		
			<h:form>
				<h:messages errorClass="errors" style="color:#ff1111" />
				<h:outputText value="#{controller.error ? (not empty controller.errors? controller.errors[0]:''):''}"/>
				<h:panelGrid>
					<h:outputText value="Please correct above errors..." style="background-color: orange; font-weight:bold"/>
					<h:commandButton value="OK" onclick="Richfaces.hideModalPanel('attendentErrorsWindow'); return false;"/>
				</h:panelGrid>
			</h:form>
		</rich:modalPanel>
		
	</ui:define>
</ui:composition>